package com.theydream.ichingwisdom.tab;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.theydream.ichingwisdom.ui.Author;
import com.theydream.ichingwisdom.ui.Cards;
import com.theydream.ichingwisdom.ui.Divination;
import com.theydream.ichingwisdom.ui.IChing;



public class Tab {
	// private static final String TAG = Tab.class.getSimpleName();
	private int resourceIcon;
	private int resourceIconSelected = 0;
	private int resourceTagTitle;

	private Activity context;
	private Intent intent;

	private View view;
	private ImageView btn;
	private String tabTag;


	public int preferedHeight = -1;
	private boolean isSelected;
	private int transparentResourceID;
	private Dialog dialog;
	private int requestCode = -1;

	public Tab(Activity context, String tabTag) {
		if (context == null) {
			throw new IllegalStateException("Context can't be null");
		}
		this.tabTag = tabTag;
		this.context = context;
	}

	public void setIcon(int resourceIcon) {
		this.resourceIcon = resourceIcon;

	}

	public void setIconSelected(int resourceIcon) {
		this.resourceIconSelected = resourceIcon;
	}

	public void setTagTitle(int resourceTitle){
		this.resourceTagTitle=resourceTitle;
	}

	public void setIntent(Intent intent, int requestForResult) {
		this.intent = intent;
		this.requestCode = requestForResult;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public Intent getIntent() {
		return intent;
	}

	public View getView() {

		if (view == null) {

			createView();
		}
		return view;
	}

	private void createView() {
		//btn = new ImageButton(context);
		btn= new ImageView(context);
		//	btn.setMaxHeight(preferedHeight);
		//	btn.setMinimumHeight(preferedHeight);
		
		btn.setPadding(0, 0, 0, 0);
		int iconId = resourceIcon;
		if (isSelected && resourceIconSelected != 0) {
			iconId = resourceIconSelected;
		}
		//		Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
		//				iconId);

		Drawable icon=context.getResources().getDrawable(iconId);
		//	btn.setImageBitmap(icon);
		//		btn.setBackgroundResource(iconId);
		btn.setBackgroundColor(Color.TRANSPARENT);
		
		//=====================================Here To Set Text========================================
		//btn.setTextColor(Color.WHITE);
		//btn.setText(resourceTagTitle);
		//btn.setText("");
		//btn.setCompoundDrawablePadding(iButterflyUtil.pixToDp(-20, context));
		//btn.setBackgroundDrawable(icon);
		btn.setImageDrawable(icon);
		//btn.setCompoundDrawablesWithIntrinsicBounds(null, null, null, icon);
		//btn.set
		bindListeners();
		view = btn;
	}

	/**Action when click on button**/
	private void bindListeners() {
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (intent == null && dialog == null) {
					Toast.makeText(context,
							"Intent or Dialog not set for '" + tabTag + "'",
							Toast.LENGTH_SHORT).show();
					// context.finish();
				} else if (intent != null && dialog != null) {
					Toast.makeText(
							context,
							" Only ONE can be set Intent or Dialog for '"
							+ tabTag + "'", Toast.LENGTH_SHORT).show();
				} else {
					if(!isSelected){//start new activity when the new activity is not the current one
						if (intent != null) {
							if (requestCode != -1) {
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								context.startActivityForResult(intent, requestCode);
								context.finish();
							} else {	
								if(requestCode==1){//no transition animation when going to catch
									intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
									intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
									context.startActivity(intent);
									context.finish();
									context.overridePendingTransition(0, 0);
								}
								else{
									intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
									context.startActivity(intent);
									context.finish();
								}
							}
						} else if (dialog != null) {
							dialog.show();
						}
					}
					else{//For going back to category main pages
						if(requestCode==0){//allow going back to collection main page when inside collection sub pages
							if(!context.getClass().equals(Author.class)){
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								context.startActivity(intent);
								context.finish();
							}
						}
						if(requestCode==2){//allow going back to map main page when inside news
							if(!context.getClass().equals(IChing.class)){
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								context.startActivity(intent);
								context.finish();
							}
						}
						if(requestCode==1){//allow going back to map main page when inside news
							if(!context.getClass().equals(Cards.class)){
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								context.startActivity(intent);
								context.finish();
							}
						}
						if(requestCode==3){//allow going back to map main page when inside news
							if(!context.getClass().equals(Divination.class)){
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								context.startActivity(intent);
								context.finish();
							}
						}
					}

				}

			}
		});

		btn.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View view, MotionEvent e) {
				if (e.getAction() == MotionEvent.ACTION_DOWN) {
					btn.setBackgroundColor(0x200000FF);
				} else if (e.getAction() == MotionEvent.ACTION_UP) {
					// Set back to transparent color 00==Full transparencty 255
					// == none
					btn.setBackgroundColor(0x00FFFFFF);
				}
				return false;
			}
		});
	}

	public String getTag() {
		return tabTag;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public void setTransparentResourceID(int transparentResourceID) {
		this.transparentResourceID = transparentResourceID;
	}

	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}
}